import { toSuccess, useEntityManager } from "@sker/core"
import { AutoDevice } from "@sker/entities"
import { useData } from "@sker/runtime"
import { FindOptionsWhere } from "typeorm"
import { z } from "zod"


export default async () => {
    const input = z.object({
        uid: z.string()
    })
    const body = input.parse(useData())
    const where: FindOptionsWhere<AutoDevice> = {};
    if (body.uid !== 'all') {
        where.uid = parseInt(body.uid);
    }
    const list = await useEntityManager(async m => {
        return await m.find(AutoDevice, { where, order: { device_name: 'asc' } })
    })
    return toSuccess(list, list.length)
}
